<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Cat extends Model
{
    //

    protected $table = 'cats';



    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
   // protected $fillable = ['name', 'email', 'password','mobile'];


    //时间戳
    public $timestamps = false;


   // public $data;

    public function __construct()
    {
        parent::__construct();
      

    }

    //找子孙栏目
    protected function findTree($data,$id=0,$lev=0)
    {
        static $tree=[];
        foreach($data as $k=>$v){
            if($v->pid==$id){
                $v->lev = $lev;
                $tree[] = $v;
                $this->findTree($data,$v->id,$lev+1);
            }
        }
        return $tree;
    }


    //格式化取所有栏目
    protected function findAll()
    {
        $data=self::select('id','name','pid')->get();
        return  $this->findTree($data,0,0);
        
    }


    public function findSon($data,$id=0)
    {
        $sons=[];
        foreach($data as $v){
            if($v->pid == $id){
                $sons[] = $v;
            }
        }
        return $sons;
    }

}


 

